package com.sunny.myproject.system.action;

import java.util.ArrayList;
import java.util.List;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.system.entity.Role;
import com.sunny.myproject.system.entity.User;
import com.sunny.myproject.system.service.RoleService;
import com.sunny.myproject.system.service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author sunnynie
 * @email sunnynie89@gmail.com
 */
@Controller
@RequestMapping("/system/user")
public class UserAction extends BaseAction<User> {

    private Logger logger = Logger.getLogger(UserAction.class);
    
    @Autowired
    private UserService service;
    
    @Autowired
    private RoleService roleService;

    @Override
    @RequestMapping(params = "list")
    public String list(Model model, HttpServletRequest request, HttpServletResponse response) {

        super.pager = super.pageStart(model, request);

        super.pager = super.pageEnd(model, service.num(criterions), super.pager);

        super.returnValue(model, service.find(criterions, super.pager.getFirstResults(), super.pager.getMaxResults()));
        return "system/userList";
    }

    @Override
    @RequestMapping(params = "add")
    public String add(HttpServletRequest reuqest, HttpServletResponse response) {
        return "system/userEdit";
    }

    @Override
    @RequestMapping(params = "edit")
    public String edit(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            super.returnValue(model, service.findById(super.param1(request)));
        } catch (Exception e) {
            logger.error(e);
        }
        return "system/userEdit";
    }

    @Override
    @RequestMapping(params = "delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        try {
            service.deleteByIds(super.param2(request));
        } catch (Exception e) {
            logger.error(e);
        }
        return "redirect:/system/user?list";
    }

    @Override
    @RequestMapping(params = "save")
    public @ResponseBody
    String save(HttpServletRequest request, HttpServletResponse response,@ModelAttribute User t) {
    	try {
    		//保存角色
    		String role_id = request.getParameter("role_id");
    		if(role_id!=null&&role_id.length()>0){
    			List<Role> roles = new ArrayList<Role>();
    			for(String id:role_id.split(",")){
    				Role role = roleService.findById(Long.parseLong(id));
    				roles.add(role);
    			}
    			t.setRoleList(roles);
    		}
    		service.save(t);
        } catch (Exception e) {
            logger.error(e);
        }
        return "system/user?list";
    }

    @Override
    @RequestMapping(params = "view")
    public String view(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            super.returnViewValue(model, service.findById(super.param1(request)));
        } catch (Exception e) {
            logger.error(e);
        }
        return "system/userEdit";
    }
}
